﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.ComponentModel;

namespace Tickz.Netz.Mailz
{
    public class TickzSendMail
    {

        /**/
        /// <summary>
        /// 发送Email功能函数
        /// </summary>
        /// <param name="from">发送人</param>
        /// <param name="to">接收人</param>
        /// <param name="cc">抄送</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="isBodyHtml">是否是HTML格式Mail</param>
        /// <returns>是否成功</returns>
        /// <param name="uName">SMTP服务用户名</param>
        /// <param name="uPass">SMTP服务密码</param>
        public static bool SendEmail(string from, string to, string cc, string subject, string body, bool isBodyHtml, string smtpuName, string smtpuPass, string smtpServer)
        {
            try
            {
                //Debug.WriteLine(DateTime.Now);
                //设置smtp
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer);
                //设置帐户
                smtp.Credentials = new NetworkCredential(smtpuName, smtpuPass);
                //开一个Message
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                //主题
                mail.Subject = subject;
                mail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                //发送
                mail.From = new System.Net.Mail.MailAddress(from);
                //邮件等级
                mail.Priority = System.Net.Mail.MailPriority.High;
                //设置邮件格式
                mail.IsBodyHtml = isBodyHtml;
                //主题内容
                mail.Body = body;
                //设置发送对象
                mail.To.Add(to);

                //如果有抄送人就抄送,没有就不送
                if (cc != null && cc != string.Empty)
                {
                    //抄送
                    mail.CC.Add(cc);
                }

                //发送Email
                smtp.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);

                object userState = mail;


                smtp.SendAsync(mail, userState);

                return true;
            }
            catch (Exception )
            {

                return false;
            }
        }

        public static void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            bool mailSent = true;

            System.Net.Mail.MailMessage mail = (System.Net.Mail.MailMessage)e.UserState;
            string subject = mail.Subject;
            string content = mail.Body;

            if (e.Cancelled)
            {
                mailSent = false;
            }
            if (e.Error != null)
            {
                mailSent = false;
            }
            if (!mailSent)
            {
                string file = "Send Mail/Send Mail test asnc Error " + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

                StringBuilder sb = new StringBuilder();
                sb.Append("Send Failed!  ");
                sb.Append("Subject :" + subject + " ");
                sb.Append("Content :" + content);
                sb.Append("Exception :" + e.Error.ToString());

            }
            else
            {
                string file = "Send Mail/Send Mail test asnc Error " + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

                StringBuilder sb = new StringBuilder();
                sb.Append("Send Sucess!  ");
                sb.Append("Subject :" + subject + " ");
                sb.Append("Content :" + content);

            }

        }
    }
}
